import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

// import '../event/event_param.dart';
// import '../tools/tools_dictonary.dart';
import '../event/event_param.dart';
import '../tools/tools_dictionary.dart';
import '../tools/tools_subtitle.dart';

// 字幕组件
class WidgetSubtitle extends StatefulWidget {
  WidgetSubtitle(
      {super.key,
      this.subtitle = "",
      this.showSubtitle = false,
      this.inputWords,
      this.textColor = const Color(0xFFFFFFFF),
      this.wrongTextColor = const Color(0xFF000000),
      this.showUnderLine = false,
      this.undLineColor = const Color(0xFFFFFFFF),
      this.height = 60,
      this.fontSize = 20.0,
      this.fontFamily,
      this.backgroundColor = const Color(0xFF000000)});

  double? height;

  // 字幕内容
  String subtitle;

  // 显示字幕
  bool showSubtitle = false;

  // 用户输入的词语列表
  List<String>? inputWords;

  // 样式
  // 字体
  String? fontFamily;

  // 字体颜色
  Color textColor;

  // 错误字体颜色(未输入正确的字体颜色)
  Color wrongTextColor;

  // 显示下划线
  bool showUnderLine;
  Color undLineColor;

  // 字体大小
  double? fontSize;

  // 背景颜色
  Color backgroundColor;

  @override
  State<WidgetSubtitle> createState() => _WidgetSubtitleState();
}

class _WidgetSubtitleState extends State<WidgetSubtitle> {
  // 字幕元素列表
  List<String>? _subtitleWords;

  // 检查是否正确
  List<bool> checkList = <bool>[];

  // 正确样式
  TextStyle? textStyle1;

  // 错误样式
  TextStyle? textStyle2;

  List<String> symbols = [",", ".", "!", "?", " "];

  @override
  void initState() {
    super.initState();

    // 初始化
    // 处理字幕
    // _subtitleWords = SubtitleTools.handleAll(widget.subtitle);

    //refreshCheck();

    textStyle1 = TextStyle(fontSize: widget.fontSize, color: widget.textColor);
    if (widget.showUnderLine) {
      textStyle2 = TextStyle(
          fontSize: widget.fontSize,
          color: widget.wrongTextColor,
          decoration: TextDecoration.underline,
          decorationStyle: TextDecorationStyle.solid,
          decorationThickness: 1,
          decorationColor: widget.undLineColor);
    } else {
      textStyle2 = TextStyle(fontSize: widget.fontSize, color: widget.wrongTextColor);
    }
  }

  @override
  Widget build(BuildContext context) {
    print("字幕" + widget.subtitle);
    // _subtitleWords = SubtitleTools.strToWords(widget.subtitle);
    _subtitleWords = ToolsSubtitle.handleAll(widget.subtitle);

    checkList = ToolsSubtitle.check(_subtitleWords!, widget.inputWords ?? []);
    print("subtitleWords:" + _subtitleWords.toString());
    print("inputWords:" + widget.inputWords.toString());
    return GestureDetector(
      child: Container(
        height: widget.height,
        width: double.infinity,
        alignment: Alignment.center,
        child: widget.showSubtitle! ? buildShow() : Text(""),
        decoration: BoxDecoration(color: widget.backgroundColor),
      ),
      onTap: () {
        print("点击");
      },
      onTapUp: (detail) {
        print("点击抬起");
      },
    );
  }

  buildShow() {
    TextStyle style = TextStyle(color: widget.textColor, fontSize: widget.fontSize);

    List<TextSpan> spans = <TextSpan>[];
    for (int i = 0; i < _subtitleWords!.length; i++) {
      TextSpan ts;
      if (checkList[i] == true) {
        ts = TextSpan(
            text: _subtitleWords![i],
            style: textStyle1,
            recognizer: TapGestureRecognizer()
              ..onTap = () {
                print("发送:" + _subtitleWords![i]);

                EventParam ep = EventParam(_subtitleWords![i]);
                ToolsDictionary.eventBus.fire(ep);
              });
      } else {
        ts = TextSpan(
            text: _subtitleWords![i],
            style: textStyle2,
            recognizer: TapGestureRecognizer()
              ..onTap = () {
                print("发送:" + _subtitleWords![i]);

                EventParam ep = EventParam(_subtitleWords![i]);
                ToolsDictionary.eventBus.fire(ep);
              });
      }
      spans.add(ts);
    }

    return SelectableText.rich(
      TextSpan(children: spans),
      // onTap: () {
      //   print("整体");
      // },
      onSelectionChanged: (TextSelection selection, SelectionChangedCause? cause) {
        print("选中变化${selection}");
      },
    );
  }

  void refreshCheck() {}
}
